//
//  WKCareListCell.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/15.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKCareListCell.h"

@interface WKCareFootView : UICollectionReusableView
@property(nonatomic,strong)UIButton *changeBtn;
@end

@implementation WKCareFootView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self layoutFootView];
    }
    return self;
}

- (void)layoutFootView{
    
    self.backgroundColor = [UIColor whiteColor];
    self.changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:self.changeBtn];
    _changeBtn.frame = CGRectMake((ScreenWidth-60)/2, 50, 60, 40);
    [_changeBtn setTitle:@"换一批" forState:UIControlStateNormal];
    [_changeBtn setTitleColor:UIColorFromHex(0xF54050) forState:UIControlStateNormal];
    [_changeBtn addTarget:self action:@selector(changeClick:) forControlEvents:UIControlEventTouchUpInside];
    _changeBtn.titleLabel.font = PFSC_MediumFont(15);
    _changeBtn.backgroundColor = [UIColor clearColor];
}

- (void)changeClick:(UIButton*)sender{
    
}

@end

@interface WKCareHeaderView : UICollectionReusableView
@property(nonatomic,strong)UILabel *titleLb;

@end

@implementation WKCareHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.titleLb = [[UILabel alloc]init];
    _titleLb.text = @"你可能感兴趣的人";
    _titleLb.font = PFSC_MediumFont(12);
    _titleLb.textColor = UIColorFromHex(0x979797);
    [self addSubview:self.titleLb];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(96, 12));
        make.center.equalTo(self);
    }];
    
    UIView *line1 = [[UIView alloc]init];
    line1.backgroundColor = UIColorFromHex(0xF8F8F8);
    [self addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(44, 1));
        make.right.mas_equalTo(_titleLb.mas_left).offset(-12);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = UIColorFromHex(0xF8F8F8);
    [self addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(44, 1));
        make.left.mas_equalTo(_titleLb.mas_right).offset(12);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
}

@end

@interface WKCarepersonCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView *headerImgView;
@property(nonatomic,strong)UILabel *nameLb;
@property(nonatomic,strong)UIButton *guanzhuBtn;
@property(nonatomic,copy)void (^gzBlock)(void);
@end

@implementation WKCarepersonCell


- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI{
    
    [self.contentView addSubview:self.headerImgView];
    [self.headerImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(60, 60));
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
    
    _headerImgView.layer.masksToBounds = YES;
    _headerImgView.layer.cornerRadius = 30;
    
    [self.contentView addSubview:self.nameLb];
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headerImgView.mas_bottom).offset(15);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(15);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
    
    
    self.guanzhuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.guanzhuBtn];
    [_guanzhuBtn setTitle:@"+关注" forState:UIControlStateNormal];
    [_guanzhuBtn setTitle:@"已关注" forState:UIControlStateSelected];
    [_guanzhuBtn setTitleColor:UIColorFromHex(0xF54050) forState:UIControlStateNormal];
    [_guanzhuBtn setTitleColor:UIColorFromHex(0xFFFFFF) forState:UIControlStateSelected];
    _guanzhuBtn.titleLabel.font = PFSC_RegularFont(11);
    _guanzhuBtn.layer.masksToBounds = YES;
    _guanzhuBtn.layer.cornerRadius = 5;
    _guanzhuBtn.layer.borderWidth = 1;
    _guanzhuBtn.layer.borderColor = UIColorFromHex(0xF54050).CGColor;
    [_guanzhuBtn addTarget:self action:@selector(guanzhuClick:) forControlEvents:UIControlEventTouchUpInside];
    _guanzhuBtn.backgroundColor = [UIColor whiteColor];
    
    [self.guanzhuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nameLb.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 24));
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
    
    [_headerImgView sd_setImageWithURL:[NSURL URLWithString:@"http://img4q.duitang.com/uploads/item/201505/24/20150524235628_jcASk.jpeg"]];
    _headerImgView.contentMode = UIViewContentModeScaleAspectFill;
    _headerImgView.layer.masksToBounds = YES;
    _nameLb.text = @"安妮海瑟薇";
}

#pragma mark - 点击关注
- (void)guanzhuClick:(UIButton*)sender{
    sender.selected = !sender.selected;
    _guanzhuBtn.backgroundColor = sender.selected == YES ? UIColorFromHex(0xF54050) : [UIColor whiteColor];
    //    _guanzhuBtn.layer.borderColor = sender.selected == YES ? UIColorFromHex(0xF54050).CGColor : [UIColor whiteColor].CGColor;
    if (self.gzBlock) {
        self.gzBlock();
    }
}

- (UIImageView *)headerImgView{
    if (!_headerImgView) {
        _headerImgView = [[UIImageView alloc]init];
    }
    return _headerImgView;
}

- (UILabel *)nameLb{
    if (!_nameLb) {
        _nameLb = [[UILabel alloc]init];
        _nameLb.textColor = UIColorFromHex(0x3C3C3C);
        _nameLb.font = PFSC_RegularFont(15);
        _nameLb.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLb;
}


@end

@implementation WKCareListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
    }
    return self;
}

- (void)setup{
    
    CGFloat width = ScreenWidth/3;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(width, 130);
    layout.minimumLineSpacing = 30;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.sectionHeadersPinToVisibleBounds = NO;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0,ScreenWidth,ScreenHeight-NavBarHeight - TabBarHeight) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    
    
    _collectionView.scrollsToTop = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.pagingEnabled = YES;
    
    [_collectionView registerClass:[WKCarepersonCell class] forCellWithReuseIdentifier:NSStringFromClass([WKCarepersonCell class])];
    
    [_collectionView registerClass:[WKCareHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([WKCareHeaderView class])];
    
    [_collectionView registerClass:[WKCareFootView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([WKCareFootView class])];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 9;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WKCarepersonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WKCarepersonCell class]) forIndexPath:indexPath];
    cell.gzBlock = self.gzBlock;
    return cell;
}

- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        WKCareHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([WKCareHeaderView class]) forIndexPath:indexPath];
    
        return headerView;
    }
    
    WKCareFootView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:NSStringFromClass([WKCareFootView class]) forIndexPath:indexPath];
    return footView;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(ScreenWidth, 72);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(ScreenWidth, 100);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
